package com.hospital.phyexam.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.phyexam.dao.*;
import com.hospital.phyexam.entity.Phyexam;
import com.hospital.phyexam.entity.ProjectResult;
import com.hospital.phyexam.entity.RelationPhyBatchKey;
import com.hospital.phyexam.entity.Student;
import com.hospital.phyexam.service.ResaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: Hospital
 * @description: 体检结果实现
 * @author: 杨曼珍
 * @create: 2019-01-13 20:11
 **/
@Service
public class ResaultServiceImpl implements ResaultService {

    @Autowired
    PhyexamProjectMapper projectMapper;

    @Autowired
    RelationPhyBatchMapper relationMapper;

    @Autowired
    PhyexamMapper phyexamMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    PhyexamBatchMapper batchMapper;
    public List getAll(Phyexam phyexam){
        List<Map<String,String>>resaultmap=new ArrayList<>();
       List<RelationPhyBatchKey>relationPhyBatchKeys=relationMapper.selectByBatchId(phyexam.getPhyExmBatchId());
       for (RelationPhyBatchKey relationPhyBatchKey:relationPhyBatchKeys){
           Map<String,String> map=new HashMap<>();
          map.put("proName",projectMapper.selectByPrimaryKey(relationPhyBatchKey.getRltPhyProId()).getProName());
           map.put("phyExmBatchName",batchMapper.selectByPrimaryKey(relationPhyBatchKey.getRltPhyExamBatchId()).getPhyExmBatchName());
           map.put("stuName",studentMapper.selectById(phyexam.getPhyExmStuId()).getStuName());
           map.put("phyExmConclusion",phyexam.getPhyExmConclusion());
           map.put("id",phyexam.getId().toString());
           resaultmap.add(map);

       }
       return resaultmap;
    }

    @Override
    public String getAllResault(){
        List<Map<String,String>>resaultmap=new ArrayList<>();
        List<Phyexam>phyexams=phyexamMapper.selectAll();
        for (Phyexam phyexam:phyexams){
            resaultmap.addAll(getAll(phyexam));
        }
        return JSON.toJSONString(resaultmap);
    }

   /* @Override
    public String insert(){
        List<Map<String,String>>resaultmap=new ArrayList<>();
        List<Phyexam>phyexams=phyexamMapper.selectAll();
        for (Phyexam phyexam:phyexams){
            resaultmap.addAll(getAll(phyexam));
        }
        return JSON.toJSONString(resaultmap);
    }*/
    @Override
    public String update(int phyexamId,String con){
        Phyexam phyexam=phyexamMapper.selectByPrimaryKey(phyexamId);
        phyexam.setPhyExmConclusion(con);
        phyexamMapper.updateByPrimaryKey(phyexam);


        return getAllResault();
    }
    @Override
    public String delete(int phyexamId){
        phyexamMapper.deleteByPrimaryKey(phyexamId);
        return getAllResault();
    }
    @Override
    public String selectByBatch(int batId){
        List<Map<String,String>>resaultmap=new ArrayList<>();
        List<Phyexam>phyexams=phyexamMapper.selectByBatchId(batId);
        for (Phyexam phyexam:phyexams){
            resaultmap.addAll(getAll(phyexam));
        }
        return JSON.toJSONString(resaultmap);
    }
    @Override
    public String selectByPro(int proId){
        List<Map<String,String>>resaultmap=new ArrayList<>();
        List<RelationPhyBatchKey>relationPhyBatchKeys=relationMapper.selectByProId(proId);
        for (RelationPhyBatchKey relationPhyBatchKey:relationPhyBatchKeys){
            List<Phyexam>phyexams=phyexamMapper.selectByBatchId(relationPhyBatchKey.getRltPhyExamBatchId());
            for (Phyexam phyexam:phyexams){
                resaultmap.addAll(getAll(phyexam));
            }

        }
        return JSON.toJSONString(resaultmap);
    }
    @Override
    public String selectByDepartment(int depId){
        List<Map<String,String>>resaultmap=new ArrayList<>();
        List<Student>students=studentMapper.selectByDepartId(depId);
        for (Student student:students){
            List<Phyexam>phyexams=phyexamMapper.selectByStuId(student.getId());
            for (Phyexam phyexam:phyexams){
                resaultmap.addAll(getAll(phyexam));
            }


        }
        return JSON.toJSONString(resaultmap);

    }


}
